# -*- coding: utf-8 -*-

"""
    (c) 2020 - Copyright ...
    
    Authors:
        zPlus <zplus@peers.community>
"""

import logging
import requests

log = logging.getLogger(__name__)

# List of HTTP headers used by ActivityPub.
# https://www.w3.org/TR/activitypub/#server-to-server-interactions
default_header  = 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
optional_header = 'application/activity+json'
headers         = [ default_header, optional_header ]

# Headers to use when GETting/POSTing remote objects
REQUEST_HEADERS = { 'Accept':       default_header,
                    'Content-Type': default_header}

# The JSON-LD context to use with ActivityPub activities.
jsonld_context = [
    'https://www.w3.org/ns/activitystreams',
    'https://w3id.org/security/v1',
    'https://forgefed.peers.community/ns' ]

# Fetch an ActivityPub object
def fetch(uri):
    response = requests.get(uri, headers=REQUEST_HEADERS)
    
    assert response.status_code == 200, \
           '[{}] Error while retrieving remote object: {}'.format(response.status_code, uri)
    
    # The remote object is expected to be a JSON-LD document.
    return response.json()

# Cache a copy of the JSON-LD context such that we can work with activities
# without sending HTTP requests all the time.
cached_jsonld_context = []
for context in jsonld_context:
    cached_jsonld_context.append(requests.get(context, headers=REQUEST_HEADERS).json())
